/** 包含应用中所有接口请求函数的模块
 * 每个函数的返回值都是promise
 * 
 * 
*/

import ajax from './ajax'
import jsonp from 'jsonp'
import { message } from 'antd';
const BASE = 'http://localhost:3000'

// 登录
export const reqLogin = (username, password) => ajax(BASE + '/login', { username, password }, "POST");

// 添加用户
export const reqAddUser = (user) => ajax(BASE + '/manage/user/add', user, "POST");


// 请求天气信息
// json请求的接口请求函数   用的jsonp的库, yarn add jsonp
export const reqWeather = (city) => {
    return new Promise((resolve,reject) => {
        const url = `http://api.map.baidu.com/telematics/v3/weather?location=${city}&output=json&ak=3p49MVra6urFRGOT9s8UBWr2`;
        // 发送jsonp请求
        jsonp(url, {}, (err, data) => {
          // 成功
          if (!err && data.status === "success") {
            const { dayPictureUrl, weather } = data.result[0].weather_data[0];
            resolve({ dayPictureUrl, weather });
          } else {
            // 失败了
            message.error('获取天气信息失败')
          }
        });
    })
}

// 获取一级或二级分类的列表
export const reqCategorys = (parentId) => ajax(BASE + "/manage/category/list", { parentId }, "GET");

// 添加分类
export const reqAddCategory = (categoryName,parentId) => ajax(BASE + "/manage/category/add", { categoryName, parentId }, "POST");

// 编辑分类
export const reqUpdateCategory = ({ categoryId, categoryName }) => ajax(BASE + "/manage/category/update", { categoryId, categoryName }, "POST");
